package com.erp.app.admin.systemMenu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import com.erp.app.admin.systemMenu.beans.SystemMenuObjectVO;
import com.erp.app.admin.systemMenu.ejb.SystemMenuGroupPK;
import com.erp.app.admin.systemMenu.ejb.SystemMenuObjectPK;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.logger.Logger;

public class SystemMenuObjectDAO extends SimplePageEditStyleDao {

	protected String sqlPurge(){
        return sqlDelete();
    }

    protected void setValuesForPurge(PreparedStatement pStmt, Object obj) throws SQLException {
        setValuesForDelete(pStmt, obj);
    }

    protected String sqlInsert() {
        return "INSERT INTO T_MENU_OBJECT (" + "COMPANY_CODE, GROUP_ID, MENU_ID, OBJECT_ID, OBJECT_NAME, OBJECT_TYPE"
                + ", OBJECT_ACTION, CREATE_USER, CREATE_DATE) " + "VALUES (?,?,?,?,?,?,?,?,sysdate)";
    }

    protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException {
        SystemMenuObjectVO menuObjectVO = (SystemMenuObjectVO) obj;
        int i = 1;
        pStmt.setString(i++, menuObjectVO.getCompanyCode());
        pStmt.setString(i++, menuObjectVO.getGroupId());
        pStmt.setString(i++, menuObjectVO.getMenuId());
        pStmt.setString(i++, menuObjectVO.getObjectId());
        setNullableString(pStmt, i++, menuObjectVO.getObjectName());
        setNullableString(pStmt, i++, menuObjectVO.getObjectType());
        setNullableString(pStmt, i++, menuObjectVO.getObjectAction());
        setNullableString(pStmt, i++, menuObjectVO.getCreateUser());
    }

    protected String sqlUpdate() {
        return "UPDATE T_MENU_OBJECT SET " + "OBJECT_NAME=?, OBJECT_TYPE=?, OBJECT_ACTION=?, UPDATE_USER=?, UPDATE_DATE=sysdate "
                + "WHERE COMPANY_CODE = ? " + "AND GROUP_ID = ? " + "AND MENU_ID = ? " + "AND OBJECT_ID = ? "
                + "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";
    }

    protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException {
        SystemMenuObjectVO menuObjectVO = (SystemMenuObjectVO) obj;
        int i = 1;
        setNullableString(pStmt, i++, menuObjectVO.getObjectName());
        setNullableString(pStmt, i++, menuObjectVO.getObjectType());
        setNullableString(pStmt, i++, menuObjectVO.getObjectAction());
        setNullableString(pStmt, i++, menuObjectVO.getUpdateUser());
        pStmt.setString(i++, menuObjectVO.getCompanyCode());
        pStmt.setString(i++, menuObjectVO.getGroupId());
        pStmt.setString(i++, menuObjectVO.getMenuId());
        pStmt.setString(i++, menuObjectVO.getObjectId());
        setNullableTimestamp(pStmt, i++, menuObjectVO.getUpdateDate());
    }

    protected String sqlDelete() {
        return "DELETE FROM T_MENU_OBJECT" + " WHERE COMPANY_CODE = ? " + " AND GROUP_ID = ? " + " AND MENU_ID = ? "
                + " AND OBJECT_ID = ? ";
    }

    protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException {
        SystemMenuObjectVO menuObjectVO = (SystemMenuObjectVO) obj;
        int i = 1;
        pStmt.setString(i++, menuObjectVO.getCompanyCode());
        pStmt.setString(i++, menuObjectVO.getGroupId());
        pStmt.setString(i++, menuObjectVO.getMenuId());
        pStmt.setString(i++, menuObjectVO.getObjectId());
    }

    protected String sqlFind() {
        return "SELECT COMPANY_CODE, GROUP_ID, MENU_ID, OBJECT_ID, OBJECT_NAME, OBJECT_TYPE"
                + ", OBJECT_ACTION, CREATE_USER, CREATE_DATE, UPDATE_USER, UPDATE_DATE " + "FROM T_MENU_OBJECT "
                + "WHERE COMPANY_CODE = ? " + "AND GROUP_ID = ? " + "AND MENU_ID = ? " + "AND OBJECT_ID = ? ";

    }

    protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException {
        SystemMenuObjectPK pk = (SystemMenuObjectPK) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
        pStmt.setString(i++, pk.getGroupId());
        pStmt.setString(i++, pk.getMenuId());
        pStmt.setString(i++, pk.getObjectId());
    }

    protected String sqlList() {
        return "SELECT COMPANY_CODE, GROUP_ID, MENU_ID, OBJECT_ID, OBJECT_NAME, OBJECT_TYPE"
                + ", OBJECT_ACTION, CREATE_USER, CREATE_DATE, UPDATE_USER, UPDATE_DATE " + "FROM T_MENU_OBJECT "
                + "WHERE COMPANY_CODE = ? " + "AND GROUP_ID = ? " + "AND MENU_ID = ? "
                + " ORDER BY COMPANY_CODE, GROUP_ID, MENU_ID, OBJECT_ID ";
    }

    protected void setValuesForList(PreparedStatement pStmt, Object obj) throws SQLException {
        SystemMenuGroupPK pk = (SystemMenuGroupPK) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
        pStmt.setString(i++, pk.getGroupId());
        pStmt.setString(i++, pk.getMenuId());
    }

    public Collection findByCriteria(Object obj, Connection conn) {
        // set the search criteria class
        // CompanyMasterSrchCriteria criteria = (CompanyMasterSrchCriteria) obj;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList rslt = new ArrayList();
        // String whereClause = toWhereClause(criteria);
        try {
            String query = "SELECT COMPANY_CODE, GROUP_ID, MENU_ID, OBJECT_ID, OBJECT_NAME, OBJECT_TYPE"
                    + ", OBJECT_ACTION, CREATE_USER, CREATE_DATE, UPDATE_USER, UPDATE_DATE " + " FROM T_MENU_OBJECT "
                    // +whereClause
                    + "";

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            if (rs != null) {
                while (rs.next()) {
                    rslt.add((SystemMenuObjectVO) getResultSetValues(rs));
                }
            }
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

    /*
     * private String toWhereClause(CompanyMasterSrchCriteria criteria) {
     * StringBuffer whereClause = new StringBuffer();
     * 
     * if (criteria.getSrchCompanyName() != null) {
     * whereClause.append(whereClause.length()== 0? "WHERE ": "AND ");
     * whereClause.append("UPPER(SHORT_NAME) =
     * '"+criteria.getSrchCompanyName().trim().toUpperCase()+"' "); }
     * 
     * return whereClause.toString(); }
     */

    protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {
        SystemMenuObjectVO menuObjectVO = new SystemMenuObjectVO();
        menuObjectVO.setCompanyCode(rs.getString("COMPANY_CODE"));
        menuObjectVO.setGroupId(rs.getString("GROUP_ID"));
        menuObjectVO.setMenuId(rs.getString("MENU_ID"));
        menuObjectVO.setObjectId(rs.getString("OBJECT_ID"));
        menuObjectVO.setObjectName(rs.getString("OBJECT_NAME"));
        menuObjectVO.setObjectType(rs.getString("OBJECT_TYPE"));
        menuObjectVO.setObjectAction(rs.getString("OBJECT_ACTION"));
        menuObjectVO.setCreateUser(rs.getString("CREATE_USER"));
        menuObjectVO.setCreateDate(rs.getTimestamp("CREATE_DATE"));
        menuObjectVO.setUpdateUser(rs.getString("UPDATE_USER"));
        menuObjectVO.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
        return menuObjectVO;
    }

    protected int getTablesInEffect() {
        return 1;
    }

    public final ChildList selectByParent(SimpleBaseVo parent, Class childType, Connection conn) throws FindRecordException {
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        ChildList rslt = new ChildList(childType);

        try {
            for (short i = 0, j; i < getTablesInEffect(); i++) {

                pStmt = conn.prepareStatement(sqlList(i));
                setValuesForList(pStmt, parent.getPrimaryKey(), i);

                j = 0;
                rs = pStmt.executeQuery();

                while (rs.next()) {
                    if (i == 0) {
                        SimpleBaseVo childVO = (SimpleBaseVo) getResultSetValues(rs, null, i);
                        childVO.setParent(parent);
                        childVO.setIsExisting(true);
                        childVO.setIsDirty(false);

                        rslt.add(childVO);
                    } else {
                        SimpleBaseVo childVO = (SimpleBaseVo) rslt.get(j);
                        if (childVO != null) {
                            childVO = (SimpleBaseVo) getResultSetValues(rs, childVO, i);
                            rslt.set(j, childVO);
                        }
                        j++;
                    }
                }
            }
        } catch (SQLException sqe) {
            System.err.println("SystemMenuObjectDAO.selectByParent(): " + (sqe.getMessage()));
            throw new FindRecordException(sqe.getMessage());
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException sqe) {
                System.err.println("SystemMenuObjectDAO.selectByParent() [" + childType.getName() + "] closing rs: "
                        + sqe.getMessage());
            }

            try {
                if (pStmt != null)
                    pStmt.close();
            } catch (SQLException sqe) {
                System.err.println("SystemMenuObjectDAO.selectByParent() [" + childType.getName() + "] closing stmt: "
                        + sqe.getMessage());
            }
        }
        return rslt;
    }

    protected String sqlList(short i) {
        if (getTablesInEffect() == 1) {
            return sqlList();
        } else {
            throw new UnsupportedOperationException("SystemMenuObjectDAO.sqlList was not implemented.");
        }
    }

    protected void setValuesForList(PreparedStatement pStmt, Object obj, short i) throws SQLException {
        if (getTablesInEffect() == 1) {
            setValuesForList(pStmt, obj);
        } else {
            throw new UnsupportedOperationException("SystemMenuObjectDAO.setValuesForList was not implemented.");
        }
    }

    protected SimpleBaseVo getResultSetValues(ResultSet rs, SimpleBaseVo model, short i) throws SQLException {
        if (getTablesInEffect() == 1) {
            return getResultSetValues(rs);
        } else {
            throw new UnsupportedOperationException("SystemMenuObjectDAO.getResultSetValues was not implemented.");
        }
    }

    public SimpleBaseVo findByPrimaryKey(Object obj, Connection conn) {
        // dummy method
        SimpleBaseVo rslt = null;
        try {
            rslt = findByPrimaryKey((SimpleBasePk) obj, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }
        return rslt;
    }

    public final SimpleBaseVo findByPrimaryKey(SimpleBasePk pk, Connection conn) throws FindRecordException {
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        SimpleBaseVo model = null;

        try {
            for (short i = 0; i < getTablesInEffect(); i++) {

                pStmt = conn.prepareStatement(sqlFind(i));
                setValuesForFind(pStmt, pk, i);

                rs = pStmt.executeQuery();

                if (rs.next()) {
                    if (i == 0) {
                        model = (SimpleBaseVo) getResultSetValues(rs, null, i);
                    } else {
                        model = (SimpleBaseVo) getResultSetValues(rs, model, i);
                    }
                }
            }
        } catch (SQLException sqe) {
        	Logger.logExceptions(this, sqe);
            throw new FindRecordException(sqe.getMessage());
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException sqe) {
                System.err.println("SystemMenuObjectDAO.findByPrimaryKey() [" + pk.getClass().getName() + "] closing rs: "
                        + sqe.getMessage());
            }

            try {
                if (pStmt != null)
                    pStmt.close();
            } catch (SQLException sqe) {
                System.err.println("SystemMenuObjectDAO.findByPrimaryKey() [" + pk.getClass().getName() + "] closing stmt: "
                        + sqe.getMessage());
            }
        }
        return model;
    }

    protected String sqlFind(short i) {
        if (getTablesInEffect() == 1) {
            return sqlFind();
        } else {
            throw new UnsupportedOperationException("SystemMenuObjectDAO.sqlFind was not implemented.");
        }
    }

    protected void setValuesForFind(PreparedStatement pStmt, Object obj, short i) throws SQLException {
        if (getTablesInEffect() == 1) {
            setValuesForFind(pStmt, obj);
        } else {
            throw new UnsupportedOperationException("SystemMenuObjectDAO.setValuesForFind was not implemented.");
        }
    }

    public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
        // dummy method
        return selectByParent(parent, SystemMenuObjectVO.class, conn);
    }

}
